package com.guigu.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.guigu.server.FeignServer;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RestTemplateTestContoller {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignServer feignServer;

    @GetMapping("/testRest")
    @SentinelResource(value = "test",blockHandler = "exceptionHandler") // 服务熔断与降级
    public Object getTest(){
        // restful风格调用
        String forObject = restTemplate.getForObject("http://server/echo/123", String.class);
        return "restTmplate:"+forObject;
    }

    // getTest()的降级替换方法，注意传参和返回值和原方法一致，传参加入BlockException
    public Object exceptionHandler(BlockException ex) {
        ex.printStackTrace();
        return "服务降级" ;
    }


    @GetMapping("/testFeign")
    public Object getTest1(){
        // openFeign方式调用
        String echo = feignServer.echo("1234");
        return "restTmplate:"+echo;
    }

    /**
     * 测试分布式事务seata
     * @return
     */
    @GlobalTransactional(timeoutMills = 1000)
    @GetMapping("/seataTest")
    public Object seataTest(){

        String forObject = restTemplate.getForObject("http://server/echo1/123", String.class);

        String echo = feignServer.echo1("1234");

        return forObject+" "+echo;
    }




}
